<?php
/**
 *      [迷你云] (C)2009-2012 南京恒为网络科技.
 *   软件仅供研究与学习使用，如需商用，请访问www.miniyun.cn获得授权
 * 
 */
?>
<?php


class MDownloadEventController extends MApplicationComponent implements MIController {
    
    public function invoke($uri = null) {
                                parent::init ();
        $params = $_REQUEST;
        
                        $user = MUserManager::getInstance ()->getCurrentUser ();
        $device = MUserManager::getInstance ()->getCurrentDevice ();
        $this->_user_id = $user->user_id;
        $this->_user_device_id = $device->device_id;
        
        $this->_file_limit = 10000;
        if (isset ( $params ["file_limit"] )) {
            $this->_file_limit = ( int ) $params ["file_limit"];
        }
        
        $this->_locale = "bytes";
        if (isset ( $params ["locale"] )) {
            $this->_locale = $params ["locale"];
        }
                $this->_event = "0";
        if (isset ( $params ["event"] )) {
            $this->_event = $params ["event"];
        }
        
        $this->build_events ();
    }
    
    
    public function build_events() {
        $event_id = 0;
        if ($this->_event !== "0") {
            $current_event = MEvents::queryEventByEventUUID ( $this->_event );
            if ($current_event ) {
                $event_id = $current_event [0] ["id"];
            }
        }
        
        $condition = "user_id = {$this->_user_id} and id > $event_id and user_device_id != {$this->_user_device_id}";
        $count = MEvents::count ( $condition );
                                if ($this->_file_limit > 10000 && $count > 10000) {
            throw new Exception ( FXP::t ( MConst::NOT_ACCEPTABLE ), MConst::HTTP_CODE_406 );
        }
        $condition .= " limit 0,{$this->_file_limit}";
        $events = MEvents::findAll ( $condition );
        $response = array ();
        $contents = array ();
        if ($events == false || empty ( $events )) {
            $response ["contents"] = array ();
            echo json_encode ( $response );
            return ;
        }
                                foreach ( $events as $key => $event ) {
            if ($key >= $this->_file_limit)
                break;
            
            $index     = strlen ( "/{$this->_user_id}" );
            $file_path = substr_replace ( $event ["file_path"], "", 0, $index );
            $action    = (int)$event ["action"];
            $context   = $event ["context"];
            
            switch ($action) {
                case MConst::CREATE_FILE:
                case MConst::MODIFY_FILE:
                    $context = unserialize($context);
                break;
                default:
                    $context = substr_replace ( $context, "", 0, $index );;
                break;
            }
            $content = array ();
            $content["action"]  = $action;
            $content["path"]    = $file_path;
            $content["context"] = $context;
            $content["event"]   = $event["event_uuid"];
            array_push ( $contents, $content );
        }
        $response["contents"] = $contents;
        echo json_encode($response);
    }
}
?>